﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class TempCardInfoController : Controller
    {
        private TempCardInfoInfo perinfo = new TempCardInfoInfo();
        public ActionResult TempCardInfoIndex()
        {
            ViewData["GroupDept"] = GetGroupDept("-1");
            ViewData["StructrueName"] = GetStructrueName();
            ViewData["Deptment"] = GetDeptment();
            ViewData["KZT"] = GetKZT();
            return View();
        }

        /// <summary>
        /// 卡状态下拉框绑定
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetKZT()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("-1", "全部");
            dic.Add("0", "正常");
            dic.Add("2", "挂失");
            dic.Add("11", "未发卡");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDeptment()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取人事信息列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetList(PageVM pvm)
        {
            string query = " 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query += " and 1=1";
            }
            else
            {
                string condition = pvm.query;
                if (condition.Split(',')[0].ToString() != "" && condition.Split(',')[0].ToString() != null)
                {
                    query += " and PName like'%" + condition.Split(',')[0].ToString() + "%'";
                }
                if (condition.Split(',')[1].ToString() != "-1")
                {
                    query += " and DEP_ID='" + condition.Split(',')[1].ToString() + "'";
                }
                if (condition.Split(',')[2].ToString() != "" && condition.Split(',')[2].ToString() != null)
                {
                    query += " and EMPNO='" + condition.Split(',')[2].ToString() + "'";
                }
                if (condition.Split(',')[3].ToString() != "" && condition.Split(',')[3].ToString() != null)
                {
                    query += " and CARDSERIAL=" + condition.Split(',')[3].ToString();
                }
                if (condition.Split(',')[4].ToString() != "-1")
                {
                    query += " and GroupIdDept=" + condition.Split(',')[4].ToString();
                }
                if (condition.Split(',')[5].ToString() != "-1")
                {
                    query += " and 岗位编号=" + condition.Split(',')[5].ToString();
                }
                if (condition.Split(',')[6].ToString() != "-1")
                {
                    query += " and 卡状态编码=" + condition.Split(',')[6].ToString();
                }
            }
            IList<TempCardInfoInfo> perlist = perinfo.GetList(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = perinfo.GetCount(query);
            List<string> v;
            foreach (TempCardInfoInfo item in perlist)
            {
                #region
                v = new List<string>();
                v.Add(item.PID.ToString());
                v.Add(item.DeptName);
                v.Add(item.GroupName);
                v.Add(item.StructrueName);
                v.Add(item.EMPNO.ToString());
                v.Add(item.PName.ToString());
                v.Add(item.CARDSTATUS.ToString());
                v.Add(item.CARDSERIAL.ToString());
                v.Add(item.Balance.ToString());
                v.Add(item.EnrollDate.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.PID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditTempCardInfo(string id)
        {
            ViewData["Deptment"] = GetDeptment();

            ViewData["StructrueName"] = GetStructrueName();
            TempCardInfoVM vm = new TempCardInfoVM();
            TempCardInfoInfo info = new TempCardInfoInfo();
            perinfo = info.GetInfoOne(id);
            ViewData["GroupDept"] = GetGroupDept(perinfo.DEP_ID.ToString());
            vm.PID = perinfo.PID;
            vm.DEP_ID = perinfo.DEP_ID;
            vm.EMPNO = perinfo.EMPNO;
            vm.PName = perinfo.PName;
            vm.CARDSTATUS = perinfo.CARDSTATUS;
            vm.CARDSERIAL = perinfo.CARDSERIAL;
            vm.Balance = perinfo.Balance;
            vm.EnrollDate = perinfo.EnrollDate;
            vm.GroupIdDept = perinfo.GroupIdDept;
            vm.structrueId = perinfo.structrueId;
            return View(vm);
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        /// <summary>
        /// 获取岗位列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetStructrueName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetStructrueName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["structrueId"]), ds.Tables[0].Rows[i]["structrueName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDeptY3(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 保存（给人员改卡）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveTempCardInfoEdit(TempCardInfoVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                TempCardInfoInfo car = new TempCardInfoInfo()
                {
                    PID = model.PID,
                    PName = model.PName,
                    DEP_ID = model.DEP_ID,
                    EMPNO = model.EMPNO,
                    GroupIdDept = model.GroupIdDept,
                    structrueId = model.structrueId
                };
                result = perinfo.SaveTempCardInfoEdit(car);
            }
            return Json(result);
        }

        /// <summary>
        /// 人员编号验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckEmpNo(int EmpNo, int PID)
        {
            bool istongguo = perinfo.CheckEmpNo(EmpNo, PID);
            return Json(istongguo);
        }

        /// <summary>
        /// 部门名称不能为空验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckDep_ID(int Dep_ID)
        {
            bool istongguo;
            if (Dep_ID == -1)
            {
                istongguo = false;
            }
            else
            {
                istongguo = true;
            }
            return Json(istongguo);
        }
    }
}
